package com.symone.webcms.service.impl;

import com.symone.webcms.mapper.DataExtMapper;
import com.symone.webcms.model.DataExt;
import com.symone.webcms.model.DataPage;
import com.symone.webcms.mapper.DataPageMapper;
import com.symone.webcms.model.ModuleField;
import com.symone.webcms.model.vo.DataPageVO;
import com.symone.webcms.service.DataPageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.util.FrameworkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
/**
 *　　
 *   @description : ContentPage 服务实现类
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-25
 */
@Service
public class DataPageServiceImpl extends ServiceImpl<DataPageMapper, DataPage> implements DataPageService {

    @Autowired
    DataExtMapper extMapper;

    @Override
    public Page<DataPage> findPage(Page<DataPage> page, Wrapper<DataPage> contentPage) {
        List<DataPage> result = baseMapper.selectPage(page,contentPage);
        page.setRecords(result);
        return page;
    }

    @Override
    public List<DataPage> findAll() {
        Wrapper<DataPage> ew = new EntityWrapper<DataPage>();
        ew.where("deleted=0");
        return baseMapper.selectList(ew);
    }

    @Override
    public int delete(long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public boolean deleteFlagById(long id) {
        return baseMapper.deleteFlagById(id);
    }

    @Override
    public int clear() {
        return baseMapper.delete(new EntityWrapper<DataPage>().eq("deleted" ,1));
    }

    @Override
    public boolean add(HttpServletRequest request, @NotNull(message = "用户名不能为空") String userName) {
        String id = request.getParameter("id");
        String lang = request.getParameter("lang");
        String module = request.getParameter("module");
        String title = request.getParameter("title");
        String state = request.getParameter("state");

        DataPage dataPage = new DataPage();
        dataPage.setLang(lang);
        dataPage.setModule(module);
        dataPage.setTitle(title);
        dataPage.setCreateUser(userName);
        dataPage.setState(state == null ? 0 : Integer.valueOf(state));
        dataPage.setUpdateUser(userName);

        if (baseMapper.insert(dataPage) > 0) {
            List<ModuleField> fields = FrameworkUtil.getFieldsByModel(module);
            for (ModuleField field : fields) {
                String extName = field.getName();
                String extValue = request.getParameter(field.getName());

                DataExt contentExt = new DataExt();
                contentExt.setCid(dataPage.getId());
                contentExt.setExtName(extName);
                contentExt.setLang(lang);
                contentExt.setModule(module);
                contentExt.setExtValue(extValue);
                contentExt.setCreateTime(new Date());
                if (extMapper.insert(contentExt) <= 0) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean update(HttpServletRequest request, @NotNull(message = "用户名不能为空") String userName) {
        String id = request.getParameter("id");
        String lang = request.getParameter("lang");
        String module = request.getParameter("module");
        String title = request.getParameter("title");
        String state = request.getParameter("state");

        DataPage dataPage =  baseMapper.selectById(id);
        dataPage.setLang(lang);
        dataPage.setModule(module);
        dataPage.setTitle(title);
        dataPage.setCreateUser(userName);
        dataPage.setState(state == null ? 0 : Integer.valueOf(state));
        dataPage.setUpdateUser(userName);

        if (baseMapper.updateById(dataPage) > 0) {
            List<ModuleField> fields = FrameworkUtil.getFieldsByModel(module);
            for (ModuleField field : fields) {
                String extName = field.getName();
                String extValue = request.getParameter(field.getName());

                Wrapper<DataExt> wrapper = new EntityWrapper<DataExt>()
                        .eq("cid", id)
                        .and()
                        .eq("lang", lang)
                        .and()
                        .eq("module", module)
                        .and()
                        .eq("ext_name", extName);

                DataExt contentExt = new DataExt();
                contentExt.setCid(Long.valueOf(id));
                contentExt.setExtName(extName);
                contentExt.setLang(lang);
                contentExt.setModule(module);
                contentExt.setExtValue(extValue);

                if (extMapper.selectCount(wrapper) > 0) {
                    contentExt.setUpdateTime(new Date());
                    extMapper.update(contentExt, wrapper);
                } else {
                    contentExt.setCreateTime(new Date());
                    extMapper.insert(contentExt);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Page<DataPageVO> findListVO(Page<DataPageVO> page,  String code,String module,String title,String moduleName) {
        List<DataPageVO> result = baseMapper.selectVOList(page,code,module,title,moduleName);
        page.setRecords(result);
        return page;
    }

    @Override
    public boolean deleteObject(Long id) {
        DataPage obj = baseMapper.selectById(id);
        if (baseMapper.deleteById(id) > 0) {
            return extMapper.deleteObject(id,obj.getLang(),obj.getModule());
        }
        return false;
    }

}
